<?php
require_once("../sqlConn.php");

$action=$_GET['action'];

switch($action)
{
	//添加用户的请求
	case 'AddUser':
	  $username=$_POST['username'];
	  $password=$_POST['password'];
	  $qq=$_POST['qq'];
	  $cellphone=$_POST['cellphone'];
	  $sex=$_POST['sex'];
	  $emot=$_POST['emot'];
	  $college=$_POST['college'];
	  $year=$_POST['year'];
	  $month=$_POST['month'];
	  $day=$_POST['day'];
	  AddUser($username,$password,$qq,$cellphone,$sex,$emot,$college,$year,$month,$day);
	  break;
	 
  //检查用户名的请求 
   case 'CheckUser':
      $username=$_POST['username'];
	  CheckUser($username); 
	  break;
  
  //用户登录请求
  case 'GetUser':
      $username=$_POST['username'];
	  $password=$_POST['password']; 
	  GetUser($username,$password); 
	  break;
	  
 //用户退出请求
  case 'Logout':
	  Logout(); 
	  break;
  case 'GetUserInfo':
      $username=$_POST['username'];
	  GetUserInfo($username); 
	  break;
  case 'GetUserInfobyID':
      $userID=$_POST['userID'];
	  GetUserInfobyID($userID); 
	  break;  
}	

function GetUser($username,$password)
{
	if($username!=null && $password!=null)
	{
		connectDB();
		$sql="select * from user where name='$username' and password='$password'";
		$res=mysql_query($sql);
		$row=mysql_fetch_array($res);
		//登录失败
		if($row==null)
		{
			echo "false";
		}
		else
		{
		   //将用户id和用户名保存到session中  在我这里 session的使用还有问题
		   /*
		   session_start();
			$_SESSION['_userID']=$row["userID"];
			$_SESSION['_username']=$row["username"];
			$a = $_SESSION['_userID'];
			$b = $_SESSION['_username'];
				echo "   x userID =".$row["userID"]  ;	  
				echo "   x username =".  $row['username'];
				echo "   userID = $a  ";	  
				echo "   username =  $b  ";
		   */
		   //每日打卡功能

		   //根据最后登陆时间和当前时间 确定是不是需要增加经验值

		   	$now=date("Y-m-d H:i:s");
			//echo "   now =  $now <br>";
			$lastlogin = $row["lastlogin"];
			//echo "   lastlogin =  $lastlogin <br>";
			$exp = $row["exp"];
			//if( strtotime($now) - strtotime($lastlogin) >= 60*60*24 )//本次登录时间和上次登录时间在一天以外
			//{
			//	$exp++;
			//}
			$today = date("d");//当天日期
			$lastlogintimestamp=strtotime($lastlogin);
			$lastday = date("d",$lastlogintimestamp);//上次登录的日期
				/* 亚洲说这一段可以用正则表达式实现
				把那个例子的正则表达式改改，就有$fl_array = preg_grep("/-\d+\s/", $array);
				不过这个函数的输入和返回都是数组
				你得稍微注意一下
				正则表达式就用这个: /-\d+\s/
				表示匹配跟在减号后面，空格(\s)前面的数字串
				*/
			//echo "today = $today";
			//echo "lastday = $lastday";
			if($today!=$lastday)
			{
				$exp++;//不是同一天登录 则可以提升经验值 当然了 这里有一个小小的漏洞 比如不同月份的同一个日期 但是可以忽略
			}
			//刷新最后登陆时间 
			$sql="update user SET lastlogin='$now',exp='$exp' Where name='$username' and password='$password'";
			$res=mysql_query($sql);		
			echo "$row[id]";//传回userId 存入cookie
	   }
	}
	else
	{
		echo "false";
	}
}

//添加新用户
function AddUser($username,$password,$qq,$cellphone,$sex,$emot,$college,$year,$month,$day)
{
	connectDB();
	$sql="insert into user(name,password,qq,cellphone,sex,emot,college,year,month,day) values('$username','$password','$qq','$cellphone','$sex','$emot','$college','$year','$month','$day')";

	$res=mysql_query($sql);
	
	echo $res;
}

//检查用户名是否存在
function CheckUser($username)
{
	connectDB();
	$sql="select id from user where name='$username'";
	$res=mysql_query($sql);
	$num=mysql_num_rows($res);
	//用户名不存在
	if($num==0)
	{
		echo "true";
	}
	else
	{
		echo "false";
	}
}
	
//获得用户信息
function GetUserInfo($username)
{
	//echo "GetUserInfo";

	if($username!=null)
	{
		connectDB();
	
		//$sql="select id,name from user where name='$username' and password='$password'";
		$sql="select * from user where name='$username'";
		$res=mysql_query($sql);
		$row=mysql_fetch_array($res);
		//echo "GetUserInfo";
		if($row==null)
		{
			//echo "没有这个用户";
			echo "true";
		}
		else
		{
			//sex
			if($row["sex"]==1)
			{
				$row["sex"]=男;
			}
			else if($row["sex"]==2)
			{
				$row["sex"]=女;			
			}
			else if ($row["sex"]==0)
			{
				$row["sex"]=不明确;			
			}
			//emot
			if($row["emot"]==1)
			{
				$row["emot"]=单身;
			}
			else if($row["emot"]==2)
			{
				$row["emot"]=恋爱;			
			}
			else if ($row["emot"]==0)
			{
				$row["emot"]=不明确;			
			}
			$userinfo="[";
			$rowStr="{id:'".$row["id"]."',name:'".$row["name"]."',password:'".$row["password"]."',qq:'".$row["qq"]."',cellphone:'".$row["cellphone"]."',sex:'".$row["sex"]."',emot:'".$row["emot"]."',college:'".$row["college"]."',year:'".$row["year"]."',month:'".$row["month"]."',day:'".$row["day"]."',exp:'".$row["exp"]."',goodcomment:'".$row["goodcomment"]."',badcomment:'".$row["badcomment"]."'}";

			$userinfo.=$rowStr;
	
			$userinfo.="]";
			echo $userinfo;
			//echo " kas ";
		}
	}
	else
	{
		echo "false";
	}
	
}

//获得用户信息
function GetUserInfobyID($userID)
{
	//echo "GetUserInfo";

	if($userID)
	{
		connectDB();
	
		//$sql="select id,name from user where name='$username' and password='$password'";
		$sql="select * from user where id='$userID'";
		$res=mysql_query($sql);
		$row=mysql_fetch_array($res);
		//echo "GetUserInfo";
		if($row==null)
		{
			//echo "没有这个用户";
			echo "true";
		}
		else
		{
			//sex
			if($row["sex"]==1)
			{
				$row["sex"]=男;
			}
			else if($row["sex"]==2)
			{
				$row["sex"]=女;			
			}
			else if ($row["sex"]==0)
			{
				$row["sex"]=不明确;			
			}
			//emot
			if($row["emot"]==1)
			{
				$row["emot"]=单身;
			}
			else if($row["emot"]==2)
			{
				$row["emot"]=恋爱;			
			}
			else if ($row["emot"]==0)
			{
				$row["emot"]=不明确;			
			}
			$userinfo="[";
			$rowStr="{id:'".$row["id"]."',name:'".$row["name"]."',password:'".$row["password"]."',qq:'".$row["qq"]."',cellphone:'".$row["cellphone"]."',sex:'".$row["sex"]."',emot:'".$row["emot"]."',college:'".$row["college"]."',year:'".$row["year"]."',month:'".$row["month"]."',day:'".$row["day"]."',exp:'".$row["exp"]."',goodcomment:'".$row["goodcomment"]."',badcomment:'".$row["badcomment"]."'}";

			$userinfo.=$rowStr;
	
			$userinfo.="]";
			echo $userinfo;
			//echo " kas ";
		}
	}
	else
	{
		echo "false";
	}
	
}

//用户退出
function Logout()
{
	session_start();
    unset($_SESSION['_userID']);
    unset($_SESSION['_username']);
}
?>